<?php

include_once( dirname(__FILE__).'/cobject.php' );
include_once( dirname(__FILE__).'/check_fields.php' );
include_once( dirname(__FILE__).'/constant.php' );

class CField extends CObject #abstract
{

#-------------------------------------------------------------------------------------------------------------------------
	var $name 	= '';
	var $value 	= '';
	var $type	= '';

	var $ch_error = 0;
	var $ch_find_error = 0;

	var $_err_info = null;

	#не сохранять в cf_save_values
	var $no_save = false;

	#не передавать значение в шаблон
	var $no_show = false;

#-------------------------------------------------------------------------------------------------------------------------

	function CField( &$params, &$err_info, $debug = 0 ){
		global $fdc_error_str;
		$this->debug = intval($debug) ? 1 : 0;
		$this->__err_info = $err_info;
		#set errors str error
		$this->errors_str = & $fdc_error_str;
		#проверяем поля
		$this->__set_params( $params );
	}

#-------------------------------------------------------------------------------------------------------------------------

	function __set_params( &$params ){
		$params = array_change_key_case( $params, CASE_LOWER );
		#set field name
		if ( $params['name'] ) $this->name = $params['name'];
		else $this->__raise_error(__FDC_ERR_EMPTY_NAME, __FILE__, __LINE__);
		#set field type
		if ( $params['type'] ) $this->type = $params['type'];
		else $this->__raise_error(__FDC_ERR_EMPTY_TYPE, __FILE__, __LINE__);
		#set field type
		if ( $params['title'] ) $this->title = $params['title'];
		else $this->__raise_error(__FDC_ERR_EMPTY_TITLE, __FILE__, __LINE__);
		$this->value = $params['value'];
		$this->ch_error = floatval( $params['errors'] );
	}

#-------------------------------------------------------------------------------------------------------------------------

	function __check_field(){
		$this->ch_find_error = _no_error_;
		$keys = array_keys($this->__err_info);
		$index = sizeof($keys);
		for($i=0; $i<$index; $i++){
			if ( $this->ch_error & $keys[$i] ){
				if ( function_exists( $this->__err_info[$keys[$i]]['FUNC'] ) ){
					$chk = $this->__err_info[$keys[$i]]['FUNC']( $this->value );
					if ( !$chk )
					    $this->ch_find_error = $keys[$i];
				}else{					$this->__raise_error(__FDC_ERR_UNKNOWN_CHECK_FUNC, __FILE__, __LINE__, $this->__err_info[$keys[$i]]['FUNC']);
				}
			}
			if ( $this->ch_find_error ) break;
		}
	}

#-------------------------------------------------------------------------------------------------------------------------

	function set_value( $value ){
		$this->value = $value;
		$this->__check_field();
	}

#-------------------------------------------------------------------------------------------------------------------------

	function gen_template_vars( $tarea ){
		$res = array();
		$js_error = '';
		#переменные ошибок
		if ( $this->ch_error ){
			$err_keys = array_keys($this->__err_info);
			$err_index = sizeof($err_keys);
			for( $err=0; $err < $err_index; $err++ ){
				if ( $this->ch_error & $err_keys[$err] {
					$buf['ERROR_NAME'] 		= '__err_'.$this->name.$this->__err_info[$err_keys[$err]]['NAME'];
					$buf['ERROR_MESS'] 		= $this->__err_info[$err_keys[$err]]['MESS'];
					$res['CH_ERROR_LOOP'][] = $buf;
					unset($buf);
					$js_error .= $this->__err_info[$err_keys[$err]]['NAME'];
				}
			}
		}
		$non_empty = 0;
		if( ( $this->ch_error & _nonempty_ ) || ( $this->ch_error & _radio_ ) )
		    $non_empty = 1;

		$res['TAREA_NAME']		= $tarea;
		$res['TITLE'] 			= $this->title;
		$res['NAME'] 			= $this->name;
		$res['VALUE'] 			= $this->value;
		$res['JS_ERROR_PREFIX'] = $js_error;
		$res['IS_NON_EMPTY']	= $non_empty;
		return $res;
	}

#-------------------------------------------------------------------------------------------------------------------------

	function set_unique_vars( &$vars ){

	}

#-------------------------------------------------------------------------------------------------------------------------

}

?>